package dao;

import classes.TipoAusenciaServidor;
import classes.Servidor;
import conexao.GerenciadorConexao;
import erro.ConexaoException;
import erro.ConsultaException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;

public class TipoAusenciaDao {
    
     private GerenciadorConexao g;

    public TipoAusenciaDao() {
        g = GerenciadorConexao.getInstancia();
    }

    public void inserirFerias(TipoAusenciaServidor f) throws ConexaoException, ConsultaException {
        String sql = "INSERT INTO ferias () VALUES ()";
        try {
            PreparedStatement pstm = (PreparedStatement) g.connect().prepareStatement(sql);
            Date data = f.getPeriodoAquiInicial();
            f.setPeriodoAquiInicial(data);
            pstm.setString(1, f.getPrevisaoGozo());
            pstm.setString(2, f.getStatusGozo());
            pstm.setDate(3, new java.sql.Date(data.getTime()));
            pstm.setString(4, f.getObs());
            pstm.setInt(5, f.getServidor().getMatricula());
            pstm.setInt(6, f.getQtdDias());
            pstm.executeUpdate();

        } catch (SQLException ex) {
            throw new ConsultaException(ex);
        } finally {
            g.disconnect();
        }
    }

    public ArrayList<TipoAusenciaServidor> listar() throws ConexaoException, ConsultaException {
        ArrayList<TipoAusenciaServidor> lista = new ArrayList<TipoAusenciaServidor>();
        TipoAusenciaServidor f;
        String sql = "SELECT agenda_id,assunto,local,data,obs,horaI,logado,status FROM agenda order by data asc";
        try {
            Statement stm = (Statement) g.connect().createStatement();
            ResultSet rs = stm.executeQuery(sql);
            while (rs.next()) {
                f = new TipoAusenciaServidor();
 
                lista.add(f);
            }
        } catch (SQLException ex) {
            throw new ConsultaException(ex);
        } finally {
            g.disconnect();
        }
        return lista;
    }


  
    public TipoAusenciaServidor pesquisar(TipoAusenciaServidor f) throws ConexaoException, ConsultaException {
        Servidor s = new Servidor();
        String sql = "SELECT * FROM ferias where agenda_id=?";
        try {
            PreparedStatement pstm = g.connect().prepareStatement(sql);
            pstm.setInt(1, f.getId());
            ResultSet rs = pstm.executeQuery();
            if (rs.next()) {

            }
        } catch (SQLException ex) {
            throw new ConsultaException(ex);
        } finally {
            g.disconnect();
        }
        return f;
    }


    public void deletar(TipoAusenciaServidor f) throws ConexaoException, ConsultaException {

        String sql = "DELETE FROM ferias where agenda_id =?";
        try {
            PreparedStatement pstm = (PreparedStatement) g.connect().prepareStatement(sql);
            pstm.setInt(1, f.getId());
            pstm.executeUpdate();
        } catch (SQLException ex) {
            throw new ConsultaException(ex);
        } finally {
            g.disconnect();
        }
    }


    public void alterar(TipoAusenciaServidor f) throws ConexaoException, ConsultaException {

        String sql = "UPDATE ferias SET assunto=?,local=?,obs=?,data=?,horaI=?,status=? WHERE agenda_id=?";
        try {
            PreparedStatement pstm = (PreparedStatement) g.connect().prepareStatement(sql);
           
            pstm.executeUpdate();
        } catch (SQLException ex) {
            throw new ConsultaException(ex);
        } finally {
            g.disconnect();
        }
    }
    
}
